var util = require('../../libs/util.js');
var app = getApp();
var sliderWidth = 120; // 需要设置slider的宽度，用于计算中间位置
Page({
  data: {
    hasData: false,
    id:"",
    trace_data:[],
    currentPage:1,
    pageNumber:0,
    firstPageDisabled:true,
    prevPageDisabled:true,
    nextPageDisabled:false,
    lastPageDisabled:false
  },
  onPullDownRefresh: function () {
    // wx.showLoading({
    //   title: '加载中',
    // })
    // this.loadData();
  },
  stopPullDownRefresh: function () {
    // wx.stopPullDownRefresh({
    //   complete: function (res) {
    //     wx.hideToast();
    //   }
    // })
  },
  onLoad: function (e) {
    var that = this;
    wx.setNavigationBarTitle({ "title": app.appName +"--配送跟踪" });
    that.setData({
      id:e.id
    });

    that.loadData();
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },
  loadData: function () {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    wx.request({
      url: app.server + '/wechat/wxapplet/driverTrace',
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'Cookie':app.globalData.cookie
      },
      data: {
        id: that.data.id,
        currentPage: that.data.currentPage
      },
      success: function (res) {
        var vehicle_data = res.data.data[0][0], data_trace = res.data.data[1], rowNumber = Number(res.data.data[2][0].count), _data = [];
        that.setData({
          pageNumber: Math.ceil(rowNumber/50)
        })
        that.pageDisabled();
        if (res.data.success && data_trace.length > 0) {
          that.setData({
            hasData: true
          });
          for (var i = 0; i < data_trace.length;i++){
            var _item = data_trace[i];
            _item.ADD_TIME = util.dateformat(_item.ADD_TIME,'hh:mm');
            if (_item.temperature){
              if (Number(_item.temperature) > Number(vehicle_data.tp_alarm)) {
                _item.temperature = _item.temperature + '°，已超过警报温度（' + vehicle_data.tp_alarm + '°）';
                _item.temperature_class = 'warn';
              } else {
                _item.temperature = _item.temperature + '°';
              }
            };
            _data.push(_item);
          }
          that.setData({
            trace_data: _data
          })

        } else {
          that.setData({
            hasData: false
          })
        };
        that.stopPullDownRefresh();
        wx.hideLoading();
      },
      fail: function (error) {
        wx.hideLoading();
        console.log(error);
        that.setData({
          hasData: false
        })
      }
    })
  },
  prevPage:function(){
    var currentPage = Number(this.data.currentPage);
    if (currentPage > 1){
      currentPage--;
    }else{
      currentPage = 1;
    }
    this.setData({
      currentPage: currentPage
    });
    this.pageDisabled();
    this.loadData();
  },
  nextPage:function(){
    var currentPage = Number(this.data.currentPage), pageNumber = this.data.pageNumber;
    if (currentPage < pageNumber) {
      currentPage++;
    }else{
      currentPage = pageNumber;
    }
    this.setData({
      currentPage: currentPage
    });
    this.pageDisabled();
    this.loadData();
  },
  firstPage: function () {
    this.setData({
      currentPage: 1
    });
    this.pageDisabled();
    this.loadData();
  },
  lastPage: function () {
    this.setData({
      currentPage: this.data.pageNumber
    });
    this.pageDisabled();
    this.loadData();
  },
  pageDisabled:function(){
    var currentPage = Number(this.data.currentPage), pageNumber = this.data.pageNumber;
    if (pageNumber == 1){
      this.setData({
        firstPageDisabled: true,
        prevPageDisabled: true,
        nextPageDisabled: true,
        lastPageDisabled: true
      });
    }else if (currentPage == 1) {
      this.setData({
        firstPageDisabled: true,
        prevPageDisabled: true,
        nextPageDisabled: false,
        lastPageDisabled: false
      });
    } else if (currentPage < pageNumber) {
      this.setData({
        firstPageDisabled: false,
        prevPageDisabled: false,
        nextPageDisabled: false,
        lastPageDisabled: false
      });
    } else if (currentPage == pageNumber) {
      this.setData({
        firstPageDisabled: false,
        prevPageDisabled: false,
        nextPageDisabled: true,
        lastPageDisabled: true
      });
    }
  },
  backPrevPage:function(){
    wx.navigateBack({
      delta: 1
    })
  }
})
